package com.jams.goodox.activity.mine.bill;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.hangzhou.jin.customview.LoadView;
import com.jams.goodox.BaseFragment;
import com.jams.goodox.R;
import com.jams.goodox.activity.mine.bill.pinnedheaderlistview.PinnedHeaderLoadMoreListView;
import com.jams.goodox.adapter.MineBillAdapter;
import com.jams.goodox.entity.Bill;
import com.jams.goodox.entity.BillEntity;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.MineService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;

/**
 * Created by Jin on 2016/7/13.
 * Description 我的账单
 */
public class MineBillFragment extends BaseFragment implements PinnedHeaderLoadMoreListView.OnLoadMoreListener {

	@Bind(R.id.listView)
	PinnedHeaderLoadMoreListView listView;
	@Bind(R.id.swipeLayout)
	SwipeRefreshLayout swipeLayout;
	@Bind(R.id.loadView)
	LoadView loadView;

	private MineBillAdapter mAdapter;

	private String type = "2";
	public final static String TYPE = "type";

	private boolean isRefresh = false;

	public static MineBillFragment newInstance(String param) {
		MineBillFragment fragment = new MineBillFragment();
		Bundle args = new Bundle();
		args.putString(TYPE, param);
		fragment.setArguments(args);
		return fragment;
	}

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


		View rootView = inflater.inflate(R.layout.mine_bill_fragment, container, false);
		ButterKnife.bind(this, rootView);

		type = getArguments().getString(TYPE);
		listView.setOnLoadMoreListener(this);

		swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh() {
				isRefresh = true;
				loadData(true);
			}
		});

		listView.setOnItemClickListener(new PinnedHeaderLoadMoreListView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int section, int position, long id) {
				if (isRefresh)
					return;
				Bundle bundle = new Bundle();
				bundle.putString(BillDetailActivity.BILL_ID, listAll.get(section).getData().get(position).getId());
				qStartActivity(BillDetailActivity.class, bundle);
			}

			@Override
			public void onSectionClick(AdapterView<?> adapterView, View view, int section, long id) {

			}
		});

		loadView.loading();
		listAll = new ArrayList<>();
		loadData(true);

		return rootView;
	}

	private String getMonthStr() {
		return ((MineBillActivity) getActivity()).getMonthStr();
	}

	int curPage = 1;

	private List<BillEntity.Parent> listAll;

	private boolean hasMore = true;

	public void loadData(final boolean isFirstPage) {
		if (isFirstPage) {
			listAll.clear();
			curPage = 1;
		} else
			curPage++;
		enqueue(createCallService(MineService.class).getBillList(getParams(curPage + "", getMonthStr(), type)), new RetrofitCallbackListener<Bill>() {
			@Override
			public void onSuccess(Call<Bill> call, Bill result) {
				swipeLayout.setRefreshing(false);
				List<Bill.DataBean.ResultListBean> list = result.getData().getResultList();
				if (null != list) {
					if (isFirstPage) {
						hasMore = true;
						if (list.size() == 0) {
							mAdapter = new MineBillAdapter(getActivity(), listAll);
							loadView.loadCompleteNoData(R.mipmap.holder_no_bill, "暂无账单");
							listView.setAdapter(mAdapter);
							mAdapter.notifyDataSetChanged();
							return;
						} else {
							List<BillEntity.Parent> listTemp = BillEntity.parse(result).getList();
							if (listTemp != null) {
								listAll.addAll(listTemp);
								hasMore = list.size() == PAGE_SIZE;
							}
							mAdapter = new MineBillAdapter(getActivity(), listAll);
							listView.setAdapter(mAdapter);
						}
					} else {
							List<BillEntity.Parent> listTemp = BillEntity.parse(result).getList();
							if (listTemp != null) {
								if (listAll.size() > 0 && listTemp.size() > 0) {
									for (BillEntity.Parent p : listTemp) {
										if (listAll.get(listAll.size() - 1).getDate().equalsIgnoreCase(listTemp.get(0).getDate())) {
											listAll.get(listAll.size() - 1).getData().addAll(listTemp.get(0).getData());
										} else {
											listAll.add(p);
										}
									}
								}
								hasMore = list.size() == PAGE_SIZE;
							}
					}
					mAdapter.notifyDataSetChanged();
				}
				loadView.loadComplete();
				listView.onLoadMoreComplete(false);
				isRefresh = false;
			}

			@Override
			public void onError(String msg) {
				loadView.loadCompleteNoData(R.mipmap.holder_no_bill, "暂无账单");
				swipeLayout.setRefreshing(false);
			}
		});
	}

	private Map<String, String> getParams(String curPage, String month, String type) {
		Map<String, String> params = new HashMap<>();
		params.put("curPage", curPage);
		params.put("pageSize", PAGE_SIZE + "");
		params.put("type", type);
		if (!month.equalsIgnoreCase(""))
			params.put("month", month);
		return params;
	}

	@Override
	public void onLoadMore() {
		if (hasMore)
			loadData(false);
		else
			listView.onLoadMoreComplete(false);
	}
}
